package com.ecommerce.order.controller;

import com.ecommerce.common.core.ApiResult;
import com.ecommerce.common.core.PageResult;
import com.ecommerce.order.dto.*;
import com.ecommerce.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单管理控制器
 * 
 * @author ecommerce
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api/v1/orders")
@RequiredArgsConstructor
@Tag(name = "订单管理", description = "订单相关的API接口")
public class OrderController {

    private final OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping
    @Operation(summary = "创建订单", description = "创建新的订单")
    @ApiResponse(responseCode = "201", description = "创建成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = OrderResponse.class)))
    public ApiResult<OrderResponse> createOrder(
            @Parameter(description = "订单创建请求", required = true)
            @RequestBody CreateOrderRequest request) {
        // 获取当前用户ID - 这里需要从安全上下文中获取，暂时使用固定值
        Long userId = 1L;
        return orderService.createOrder(userId, request);
    }

    /**
     * 获取订单列表（分页）
     */
    @GetMapping
    @Operation(summary = "获取订单列表", description = "分页获取订单列表")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = OrderResponse.class)))
    public ApiResult<PageResult<OrderResponse>> getOrders(
            @Parameter(description = "订单查询条件")
            @ModelAttribute OrderQueryRequest query,
            @PageableDefault(size = 10) Pageable pageable) {
        // 获取当前用户ID - 这里需要从安全上下文中获取，暂时使用固定值
        Long userId = 1L;
        return orderService.pageUserOrders(userId, query);
    }

    /**
     * 根据ID获取订单详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取订单详情", description = "根据订单ID获取订单详细信息")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = OrderResponse.class)))
    public ApiResult<OrderResponse> getOrderById(
            @Parameter(description = "订单ID", required = true)
            @PathVariable Long id) {
        // 获取当前用户ID - 这里需要从安全上下文中获取，暂时使用固定值
        Long userId = 1L;
        return orderService.getOrderDetail(userId, id);
    }

    /**
     * 取消订单
     */
    @PutMapping("/{id}/cancel")
    @Operation(summary = "取消订单", description = "取消指定订单")
    @ApiResponse(responseCode = "200", description = "取消成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = OrderResponse.class)))
    public ApiResult<OrderResponse> cancelOrder(
            @Parameter(description = "订单ID", required = true)
            @PathVariable Long id) {
        // 获取当前用户ID - 这里需要从安全上下文中获取，暂时使用固定值
        Long userId = 1L;
        orderService.cancelOrder(userId, id);
        return orderService.getOrderDetail(userId, id);
    }

    /**
     * 确认收货
     */
    @PutMapping("/{id}/confirm-receipt")
    @Operation(summary = "确认收货", description = "确认收到商品")
    @ApiResponse(responseCode = "200", description = "确认成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = OrderResponse.class)))
    public ApiResult<OrderResponse> confirmReceipt(
            @Parameter(description = "订单ID", required = true)
            @PathVariable Long id) {
        // 获取当前用户ID - 这里需要从安全上下文中获取，暂时使用固定值
        Long userId = 1L;
        orderService.confirmReceive(userId, id);
        return orderService.getOrderDetail(userId, id);
    }

    /**
     * 获取订单商品列表
     */
    @GetMapping("/{id}/items")
    @Operation(summary = "获取订单商品列表", description = "获取指定订单的商品列表")
    @ApiResponse(responseCode = "200", description = "获取成功",
            content = @Content(mediaType = "application/json",
                    schema = @Schema(implementation = OrderItemResponse.class)))
    public ApiResult<List<OrderItemResponse>> getOrderItems(
            @Parameter(description = "订单ID", required = true)
            @PathVariable Long id) {
        // 获取当前用户ID - 这里需要从安全上下文中获取，暂时使用固定值
        Long userId = 1L;
        OrderResponse orderDetail = orderService.getOrderDetail(userId, id).getData();
        return ApiResult.success(orderDetail.getItems());
    }
}